package com.sunny.l16;

public class Solution {

//    public static void main(String[] args) {
//
//        int[] ratings = {4,2,3,4,1};
//        System.out.println(new Solution().candy(ratings));
//
//    }

    public int candy(int[] ratings) {

        int[] arr = new int[ratings.length];
        for(int i=0;i<arr.length;i++){
            arr[i] = 1;
        }
        for(int i=1;i<ratings.length;i++){
            if(ratings[i] > ratings[i-1]){
                arr[i] = arr[i-1]+1;
            }
        }
        for(int i=ratings.length-2;i>=0;i--){
            if(ratings[i] > ratings[i+1]){
                if(arr[i] < arr[i+1] + 1)
                    arr[i] = arr[i+1]+1;
            }
        }

        int res = 0;
        for(int tmp: arr){
            res += tmp;
        }
        return res;

    }

}
